/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.planning.tactical;

import nl.cloudfarming.client.crop.Race;
import java.util.List;
import nl.cloudfarming.client.crop.Crop;
import nl.cloudfarming.client.crop.CropService;
import org.uispec4j.ComboBox;
import junit.framework.Assert;
import java.awt.Component;
import javax.swing.JComboBox;
import java.awt.event.ActionEvent;
import org.junit.Test;
import org.openide.util.Lookup;
import org.uispec4j.Panel;
import org.uispec4j.UISpec4J;
import org.uispec4j.finder.ComponentMatcher;
import static org.mockito.Mockito.*;
        
/**
 *
 * @author Gerben Feenstra
 */
public class PlanProductionUnitActionIT {

    private final ComboBoxComponentMatcher comboBoxMatcher = new ComboBoxComponentMatcher();
        
    static {
        UISpec4J.init();
    }
        
    /**
     * Test Race combobox content when Crop has been selected in Crop combobox
     */
    @Test
    public void testComboBoxes(){

        CropService cs = Lookup.getDefault().lookup(CropService.class); 
                
        // Create Action
        PlanProductionUnitAction action = new PlanProductionUnitAction();
        ActionEvent event = mock(ActionEvent.class);
        action.actionPerformed(event);

        // Get crops, should not be null 
        List<Crop> serviceCrops = cs.getCrops();
        Assert.assertNotNull(serviceCrops);
        
        // Get first Crop + races belonging to this crop
        Crop serviceCrop = serviceCrops.get(0);
        List<Race> serviceRaces = cs.getRaces(serviceCrop);
     
        // Retrieve UISpec panel
        Panel panel = new Panel(action.getPanel());
        Assert.assertTrue(panel.containsComponent(comboBoxMatcher).isTrue());
         
        // Get UISpec comboboxes from UISpec panel
        ComboBox comboBoxCrops = panel.getComboBox(PlanProductionUnitAction.COMBOBOX_CROPS_NAME);
        ComboBox comboBoxRaces = panel.getComboBox(PlanProductionUnitAction.COMBOBOX_RACES_NAME);

        // Find servicecrop in combobox
        comboBoxCrops.select(serviceCrop.toString());
        
        // Selecting a crop should update the Races-combobox to reflect the races belonging to the selected crop.
        // hence: Races in serviceRaces should be equal to the races in comboboxRaces
        for(Race race : serviceRaces){
            comboBoxRaces.contains(race.toString()).isTrue(); 
        }        
    }
        

    class ComboBoxComponentMatcher implements ComponentMatcher {

        @Override
        public boolean matches(Component component) {
            return (component instanceof JComboBox);
        }
    }

}
